{php include wl_template('common/header');}
<link href="../addons/lexiangpingou/web/resource/css/app.css" rel="stylesheet">
<style>
.app .panel {background-color: #FFF;}
</style>
<form action="" method="post" class="form-horizontal">
	<div class="panel panel-default">
		<div class="panel-heading">设置店铺优惠券</div>
		<div class="panel-body">
			<div class="app clearfix">
				<div class="app-preview wap-editor-footer">
					<div class="app-header"></div>
					<div class="app-content app-coupon">
						<div class="inner">
							<div class="title">
								<h1><span>优惠券</span></h1>
							</div>
							<div class="con">
								<div class="ump-coupon-detail-wrap">
									<div class="promote-card">
										<div class="name clearfix">
											<span class="js-name">{if $coupon_template} {$coupon_template['name']} {else}优惠券标题{/if}</span>
											<a href="" class="promote-share hidden">分享</a>
										</div>
										<div class="text-center promote-value">
											￥ <span class="js-value">{if $coupon_template['is_random'] == 0} {$coupon_template['value']} {elseif $coupon_template['is_random'] == 1} {$coupon_template['value']} ~ {$coupon_template['value_to']} {else}0.00{/if}</span>
										</div>
										<div class="text-center promote-limit js-is-at-least">
											订单满 <span class="js-at-least"> {if $coupon_template} {$coupon_template['at_least']} {else} XX {/if}</span>元 (不含运费)
										</div>
										<div class="text-center promote-time">
											有效日期：<span class="js-start-time">{if $coupon_template} {php echo date('Y-m-d H:i:s', $coupon_template['start_time']);} {else}20xx 00 : 00{/if}</span> - <span class="js-end-time">{if $coupon_template} {php echo date('Y-m-d H:i:s', $coupon_template['end_time']);} {else} 20xx 00 : 00 {/if}</span>
										</div>
										<div class="dot"></div>
									</div>
									<div class="promote-desc">
										<div class="promote-desc-title">使用说明</div>
										<div class="desc-detail js-description">
											{if $coupon_template} {$coupon_template['description']} {else}暂无使用说明……{/if}
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="app-side">
					<div class="app-coupon-edit" >
						<div class="arrow-left"></div>
						<div class="inner">
							<div class="panel panel-default">
								<div class="panel-heading b">优惠券基础信息</div>
								<div class="panel-body">
									<div class="form-group">
										<label class="col-md-3 control-label"><span class="red">*</span>优惠券名称</label>
										<div class="col-md-9">
											<input type="text" class="form-control" name="name" value="{$coupon_template['name']}">
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label"><span class="red">*</span>发放总量</label>
										<div class="col-md-9">
											<div class="input-group">
												<input type="text" class="form-control" name="total" value="{$coupon_template['total']}">
												<span class="input-group-addon">张</span>
											</div>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label"><span class="red">*</span>面值</label>
										<div class="col-md-9">
											<div class="col-md-9 pl0">
												<input type="text" class="form-control" name="value" value="{$coupon_template['value']}" {if $do == 'edit'} disabled {/if} style="display:inline-block; width:100px;">
												<span class="js-random" {if $coupon_template['is_random'] == 0 || $do == 'create'}style="display:none;"{/if}>
												至
												<input class="form-control" type="text" value="{$coupon_template['value_to']}" name="value_to" {if $do == 'edit'} disabled {/if} style="display:inline-block; width:100px;">
												</span>
											</div>
											<div class="col-md-3 text-right" style="height:34px;">
												<label class="checkbox-inline" >
													<input type="checkbox" name="is_random" value="2" {if $coupon_template['is_random'] == ON} checked="checked" {/if} {if $do == 'edit'} disabled {/if}>
													随机
												</label>
											</div>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label"><span class="red">*</span>订单金额</label>
										<div class="col-md-9">
											<label class="radio-inline">
												<input type="radio" value="1" name="is_at_least" {if $coupon_template['is_at_least'] == OFF}checked="checked"{/if} {if $do == 'edit'} disabled {/if} />不限制
											</label>
											<br>
											<label class="radio-inline">
												<input type="radio" value="2" name="is_at_least" style="margin-top:10px;" {if $coupon_template['is_at_least'] == ON}checked="checked"{elseif empty($coupon_template['is_at_least'])} checked{/if}  {if $do == 'edit'} disabled {/if}/>
												<span>
												满
												<input class="form-control inline-item" type="text" value="{$coupon_template['at_least']}" name="at_least" {if $do == 'edit'} disabled {/if} >
												元可使用
												</span>
											</label>
										</div>
									</div>
									<div class="form-group hidden">
										<label class="col-md-3 control-label">同步发布至</label>
										<div class="col-md-9">
											<label class="checkbox-inline">
												<input type="checkbox" value="2" name="is_sync_weixin"/>微信卡券(包)
											</label>
											<span class="help-block">
												如您的微信公众号没有开通卡券权限，将由有赞代发券。同步至微信卡包后，需等待微信审核通过，才能领取；
											</span>
										</div>
									</div>
								</div>
							</div>
							<div class="panel panel-default">
								<div class="panel-heading b">基本规则</div>
								<div class="panel-body">
									<div class="form-group hidden">
										<label class="col-md-3 control-label"><span class="red">*</span>会员等级</label>
										<div class="col-md-9">
											<select name="user_level" class="form-control" {if $do == 'edit'} disabled {/if}>
												<option value="" selected="">所有会员等级</option>
												<option value="">VIP会员</option>
												<option value="">高级会员</option>
											</select>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label"><span class="red">*</span>每人限领</label>
										<div class="col-md-8">
											<select name="quota" class="form-control" {if $do == 'edit'} disabled {/if}>
												<option value="" {if empty($coupon_template['quota'])}selected=""{/if}>不限张</option>
												<option value="1" {if $coupon_template['quota'] == 1}selected=""{/if}>1张</option>
												<option value="2" {if $coupon_template['quota'] == 2}selected=""{/if}>2张</option>
												<option value="3" {if $coupon_template['quota'] == 3}selected=""{/if}>3张</option>
												<option value="4" {if $coupon_template['quota'] == 4}selected=""{/if}>4张</option>
												<option value="5" {if $coupon_template['quota'] == 5}selected=""{/if}>5张</option>
												<option value="10" {if $coupon_template['quota'] == 10}selected=""{/if}>10张</option>
											</select>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label"><span class="red">*</span>生效时间</label>
										<div class="col-md-9">
											{php echo tpl_form_field_date('start_time', $coupon_template['start_time']);}
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label"><span class="red">*</span>过期时间</label>
										<div class="col-md-9">
											{php echo tpl_form_field_date('end_time', $coupon_template['end_time']);}
										</div>
									</div>
									<div class="form-group hidden">
										<label class="col-md-3 control-label">同步打标签</label>
										<div class="col-md-6">
											<textarea name="fans_tag" class="form-control" rows="3"></textarea>
										</div>
										<div class="col-md-3">
											<a href="">刷新</a> | <a href="">新建</a>
										</div>
									</div>
									<div class="form-group hidden">
										<label class="col-md-3 control-label">到期提醒</label>
										<div class="col-md-9">
											<label class="checkbox-inline">
												<input type="checkbox" value="2" name="expire_notice"/>到期前4天提醒一次
											</label>
										</div>
									</div>
									<div class="form-group hidden">
										<label class="col-md-3 control-label">推广设置</label>
										<div class="col-md-9">
											<label class="checkbox-inline">
												<input type="checkbox" value="2" name="is_share"/>允许分享领取链接
											</label>
										</div>
									</div>
									<div class="form-group hidden">
										<label class="col-md-3 control-label"><span class="red">*</span>可使用商品</label>
										<div class="col-md-9">
											<label class="radio-inline">
												<input type="radio" value="2" name="range_type" checked="checked" />全店通用
											</label>
											<label class="radio-inline">
												<input type="radio" value="1" name="range_type"/>指定商品
											</label>
											<br>
											<label class="checkbox-inline">
												<input type="checkbox" value="2" name="is_forbid_preference" {if $do == 'edit'} disabled {/if} />仅原价购买商品时可用
											</label>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label">使用说明</label>
										<div class="col-md-9">
											<textarea maxlength="255" name="description" class="form-control" rows="3" placeholder="填写活动的详细说明,支持换行">{$coupon_template['description']}</textarea>
										</div>
									</div>
	
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="shop-preview col-xs-12 col-sm-9 col-lg-10" style="z-index: 999;">
					<div class="text-center alert alert-warning">
						<button type="button" class="btn btn-default js-back min-width">取消</button>
						<!-- <button type="submit" class="btn btn-primary min-width">保存</button> -->
						<input name="submit" type="submit" class="btn btn-primary min-width" value="保存">
						<input type="hidden" name="token" value="{$_W['token']}" />
					</div>
				</div>
			</div>
		</div>
	</div>
</form>
<script>
	$(function() {
		$('.js-back').click(function() {
			location.href = "{php echo url('activity/coupon_template/list');}";
		});
		
		var $name = $('input[name="name"]');
		var $total = $('input[name="total"]');
		var re = /^\d+$/;
		var $value = $('input[name="value"]');
		var $value_to = $('input[name="value_to"]');
		var $random = $('input[name="is_random"]:checkbox');
		var $at_least = $('input[name="at_least"]');
		var $is_at_least = $('input[name="is_at_least"]:radio');
		var $start_time = $('input[name="start_time"]');
		var $end_time = $('input[name="end_time"]');
		var $description = $('[name="description"]');

		$('form').submit(function() {

			if ($(this).find('.text-danger, .has-error').length > 0) {
				return false;
			};
			if ($name.val() == '') {
				util.tips('优惠券名称不能为空', 2000);
				$name.focus().parent().append('<span class="text-danger">优惠券名称必须在 1-10 个字内</span>').addClass('has-error');
				return false;
			};
			if (!re.test($total.val())) {
				$total.focus().parent().parent().append('<div class="text-danger">发放总量必须是一个整数</div>').addClass('has-error');
				return false;
			};
			if (isNaN(parseFloat($value.val())) || parseFloat($value.val()) < 0.01){
				$value.parent().addClass('has-error').append('<span class="text-danger">优惠券面值必须大于等于 0.01 元</span>');
				return false;
			};
			if ($random.prop('checked')) {
				if (isNaN(parseFloat($value_to.val())) || parseFloat($value_to.val()) < 0.01){
					$value_to.parent().addClass('has-error').append('<span class="text-danger">优惠券面值必须大于等于 0.01 元</span>');
					return false;
				};
				if (parseFloat($value_to.val()) <= parseFloat($value.val())) {
					$value.parent().addClass('has-error').append('<div class="text-danger">优惠券面值范围的上限必须大于下限</div>');
					return false;
				};
			};
			if ($('input[name="is_at_least"]:radio:checked').val() == 2) {
				if ($at_least.val() == '' || parseFloat($at_least.val()) < parseFloat($value.val())) {
					$at_least.parent().append('<div class="text-danger">订单限制金额必须大于等于优惠券的面值</div>');
					return false;
				};
				if (parseFloat($value_to.val()) > parseFloat($at_least.val())) {
					$at_least.parent().append('<div class="text-danger">订单限制金额必须大于等于优惠券的最大面值</div>');
					return false;
				};
			};
			if ($start_time.val() == '') {
				$start_time.parent().after('<div class="text-danger">生效时间不能为空</div>');
				return false;
			};
			if ($end_time.val() == '') {
				$end_time.parent().after('<div class="text-danger">过期时间不能为空</div>');
				return false;
			};
			var start_year_month_day = $start_time.val().split(" ")[0].split('-');
			var start_year = start_year_month_day[0];
			var start_month = start_year_month_day[1];
			var start_day = start_year_month_day[2];
			var start_hour_minute = $start_time.val().split(" ")[1].split(':');
			var start_hour = start_hour_minute[0];
			var start_minute = start_hour_minute[1];
			var start_second = 00;
			var start_time = util.DateToUnix(start_year, start_month, start_day, start_hour, start_minute, start_second);

			var end_year_month_day = $end_time.val().split(" ")[0].split('-');
			var end_year = end_year_month_day[0];
			var end_month = end_year_month_day[1];
			var end_day = end_year_month_day[2];

			var end_hour_minute = $end_time.val().split(" ")[1].split(':');
			var end_hour = end_hour_minute[0];
			var end_minute = end_hour_minute[1];
			var end_second = 00;
			var end_time = util.DateToUnix(end_year, end_month, end_day, end_hour, end_minute, end_second);

			if (parseInt(start_time) > parseInt(end_time)) {
				util.tips('结束时间必须大于开始时间', 3000);
				return false;
			};
			var date = new Date()
			var this_year = date.getFullYear()
			var this_month = date.getMonth() + 1
			var this_day = date.getDate()
			var this_hour = date.getHours();
			var this_minute = date.getMinutes();
			var this_second = date.getSeconds();
			var this_time = this_year+(this_month<10 ? "0" + this_month : this_month)+(this_day<10 ? "0"+ this_day : this_day)+(this_hour<10 ? "0"+ this_hour : this_hour)+(this_minute<10 ? "0" + this_minute : this_minute)+(this_second<10 ? "0" +this_second : this_second);
			this_time = util.DateToUnix(this_year, this_month, this_day, this_hour, this_minute, this_second);
			console.log(start_time);
			console.log(end_time);
			console.log(this_time);
			if (this_time > end_time) {
				util.tips('结束时间必须大于当前时间', 3000);
				return false;
			};
		});

		{if $do == 'edit'}
			$start_time.removeClass('datetimepicker').prop('disabled', true).parent().css('background-color', '#eee');
			$end_time.removeClass('datetimepicker').prop('disabled', true).parent().css('background-color', '#eee');
		{/if}
		$name.blur(function() {
			$(this).parent().removeClass('has-error').find('.text-danger').remove();
			if ($(this).val() == '') {
				$(this).parent().append('<span class="text-danger">优惠券名称必须在 1-10 个字内</span>').addClass('has-error');
			} else {
				$('.js-name').text($(this).val());
			};
		});
		$total.blur(function() {
			$(this).parent().parent().removeClass('has-error').find('.text-danger').remove();
			if (!re.test($(this).val())) {
				$(this).parent().parent().append('<div class="text-danger">发放总量必须是一个整数</div>').addClass('has-error');
			};
		});
		$value.blur(function() {
			$(this).parent().parent().removeClass('has-error').find('.text-danger').remove();
			var value = $(this).val();
			if ($value_to.val() && parseFloat(value) >= parseFloat($value_to.val())) {
				$(this).parent().parent().addClass('has-error').append('<div class="text-danger">优惠券面值范围的上限必须大于下限</div>');
				return false;
			};
			if (value == '') {
				$('.js-value').text('0.00');
				return false;
			} else if (isNaN(parseFloat(value)) || parseFloat(value) < 0.01){
				$(this).parent().parent().addClass('has-error').append('<div class="text-danger">优惠券面值必须大于等于 0.01 元</div>');
				return false;
			} else {
				$(this).parent().parent().removeClass('has-error').find('.text-danger').remove();
			};
			$('.js-value').text(value);
		});

		$value_to.blur(function() {
			$(this).parent().parent().parent().removeClass('has-error').find('.text-danger').remove();
			var value = $(this).val();
			if ($value.val() && parseFloat(value) <= parseFloat($value.val())) {
				$(this).parent().parent().addClass('has-error').append('<div class="text-danger">优惠券面值范围的上限必须大于下限</div>');
				return false;
			};
			if (value == '') {
				return false;
			} else if (isNaN(parseFloat(value)) || parseFloat(value) < 0.01) {
				$(this).parent().parent().parent().addClass('has-error').append('<div class="text-danger">优惠券面值范围必须大于等于 0.01 元</div>');
				return false;
			} else {
				$(this).parent().parent().removeClass('has-error').find('.text-danger').remove();
			};
			if ($value.val() == '' && value) {
				$('.js-value').text('0.00' + ' ~ ' + value);
			} else if (!isNaN($value.val()) && value) {
				$('.js-value').text($value.val() + ' ~ ' + value);
			};
			
		});
		$random.click(function() {
			$value.parent().removeClass('has-error').find('.text-danger').remove();
			if ($value.val() && isNaN(parseFloat($value.val()))) {
				$value.val('');
			};
			if ($value_to.val() && isNaN(parseFloat($value_to.val()))) {
				$value_to.val('');
			};
			if ($(this).prop('checked')) {
				$('.js-random').show();
			} else {
				if ($value.val()) {
					$('.js-value').text(parseFloat($value.val()));
				};
				$('.js-random').hide();
			};
		});
		$is_at_least.click(function() {
			$(this).parent().parent().find('.text-danger').remove().removeClass('has-error');
			$(this).parent().parent().find('span').removeClass('has-error');
			var fee = '';
			if ($(this).val() == 1) {
				$('.js-is-at-least').text('无限制');
			} else {
				if ($at_least.val()) {
					fee = $at_least.val();
				} else {
					fee = ' xx ';
				};
				$('.js-is-at-least').html('订单满 <span class="js-at-least"> ' + fee + ' </span>元 (含运费)');
			};
		});
		
		$at_least.blur(function() {
			$(this).parent().removeClass('has-error').find('.text-danger').remove();
			if (isNaN(parseFloat($(this).val())) || parseFloat($(this).val()) < 0.01) {
				$(this).parent().addClass('has-error').append('<div class="text-danger">必须大于等于 0.01 元</div>');
				return false;
			};
			if ($(this).val()) {
				$('.js-at-least').text(parseFloat($(this).val()));
			} else {
				$('.js-at-least').text('xx');
			};
		});
		$start_time.change(function(){
			$(this).parent().removeClass('has-error').next('.text-danger').remove();
			$('.js-start-time').text($(this).val());
		});
		$end_time.change(function(){
			$(this).parent().removeClass('has-error').next('.text-danger').remove();
			$('.js-end-time').text($(this).val());
		});
		$description.blur(function() {
			if ($(this).val()) {
				$('.js-description').text($(this).val());
			} else {
				$('.js-description').text('暂无使用说明……');
			};
		});
	});
</script>
{php include wl_template('common/footer');}
